const router = require('koa-router')(),
      mysql = require('mysql2/promise'),
      { img, community,course,plan} = require('../models'),
      mime = require('mime-types'),
      path = require('path'),
      fs = require('fs');

router.prefix('/api/kdoing/img');

router.post('/img', async (ctx, next) => {
  console.log(ctx.request.body);
  ctx.body = ctx.request.body;
});
router.post('/addcomm', async (ctx, next) => {
  const {comm_id, comm_text, user_id,comm_time} = ctx.request.body;
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  const file = ctx.request.files;
  let path = file.img.path.split('/');
  let img = path[path.length-1];
  ctx.body = await community.addComm(comm_id, comm_text, user_id, img, comm_time) 
  console.log(ctx.request.body)
  console.log(file)
  console.log(img)
  console.log('------------------------------');
  console.log(comm_id);
  console.log('-------------------------------------------------------------------')
});
router.post('/addcourse', async (ctx, next) => {
  console.log(ctx.request.body)
  const {course_id,course_name,classification, course_tag,course_level,course_time} = ctx.request.body;
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  const file = ctx.request.files;
  let path = file.img.path.split('/');
  let img = path[path.length-1];
  ctx.body = await course.addCourse(course_id,course_name,classification, course_tag,course_level,course_time, img)
});
router.post('/addchildcourse', async (ctx, next) => {
  const {cc_id, cc_name, course_id, } = ctx.request.body;
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  const file = ctx.request.files;
  let path = file.img.path.split('/');
  let img = path[path.length-1];
  console.log('img:   '+img);
  console.log(ctx.request.body);
  ctx.body = await course.addChildCourse(cc_id, cc_name, course_id, img)
});
router.post('/addplan', async (ctx, next) => {
  console.log(ctx.request.body);
  const {plan_id, plan_name, plan_img, plan_tag, classification, plan_week, plan_rec} = ctx.request.body;
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  const file = ctx.request.files;
  let path = file.img.path.split('/');
  let img = path[path.length-1];
  ctx.body = await plan.addPlan(plan_id, plan_name, img, plan_tag, classification, plan_week, plan_rec)
});
router.get('/getimg/:img', async (ctx, next) => {
  const {img} = ctx.params;
  let path = '/home/suhan/sql/04-cloud/image/'+img;
  file = fs.readFileSync(path);
  ctx.set('content-type', mime.lookup(path))
  ctx.body = file;
})
module.exports = router;
